package com.randi.system.service.impl.sysNotice;

import com.randi.common.config.AppConfig;
import com.randi.system.domain.entity.sysNotice.SysNoticeRead;
import com.randi.system.mapper.sysNotice.SysNoticeReadMapper;
import com.randi.system.service.ISysUserService;
import com.randi.system.service.sysNotice.ISysNoticePushService;
import com.randi.system.service.sysNotice.ISysNoticeReadService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysNoticeReadServiceImpl implements ISysNoticeReadService {

    @Resource
    private SysNoticeReadMapper sysNoticeReadMapper;

    @Resource
    private AppConfig.SystemConfig systemConfig;

    /**
     * 获取已读用户ID列表
     *
     * @param noticeId 公告ID
     * @return 已读用户ID列表
     */
    @Override
    public List<Long> selectSysNoticeReadUserIds(Long noticeId) {
        SysNoticeRead sysNoticeRead = new SysNoticeRead();
        sysNoticeRead.setNoticeId(noticeId);
        return sysNoticeReadMapper.selectSysNoticeReadList(sysNoticeRead).stream()
                .map(SysNoticeRead::getUserId)
                .collect(Collectors.toList());
    }

    /**
     * 插入公告阅读记录
     *
     * @param sysNoticeRead sysNoticeRead
     * @return 结果
     */
    @Override
    public Integer insertSysNoticeRead(SysNoticeRead sysNoticeRead) {
        Long userId = sysNoticeRead.getUserId();
        if (userId.equals(systemConfig.getUserAdminId())) {
            return 1;
        }
        sysNoticeRead.setCreateTime(new Date());
        return sysNoticeReadMapper.insert(sysNoticeRead);
    }


}
